#include "TransformationNode.h"
#include "IRenderer.h"

TransformationNode::TransformationNode( Subject<Maths::Vector3D>* position, Subject<Maths::Quaternion>* rotation )
{
	m_position = new ValueTrigger<Maths::Vector3D>(position);
	m_rotation = new ValueTrigger<Maths::Quaternion>(rotation);
}

TransformationNode::~TransformationNode()
{
}

HashedString TransformationNode::Type() const
{
	return HashedString("TransformationNode");
}

void TransformationNode::Enable()
{
	Renderer->PushModelViewMatrix(getMatrix());
	INode::Enable();
	Disable();
}

void TransformationNode::Disable()
{
	Renderer->PopModelViewMatrix();
}

Maths::Matrix TransformationNode::getMatrix()
{
	Maths::Vector3D& position = ((ValueTrigger<Maths::Vector3D>*)(TriggerBase*)m_position)->getObject();
	Maths::Quaternion& rotation = ((ValueTrigger<Maths::Quaternion>*)(TriggerBase*)m_rotation)->getObject();

	Maths::Matrix orientation;
	orientation.Translate(position);
	orientation *= rotation.GetMatrix();
	return orientation;
}
	
Pointer<TriggerBase>& TransformationNode::getPosition()
{
	return m_position;
}

Pointer<TriggerBase>& TransformationNode::getRotation()
{
	return m_rotation;
}

void TransformationNode::setPosition( Maths::Vector3D& position )
{
	*((ValueTrigger<Maths::Vector3D>*)(TriggerBase*)m_position) = position;
}

void TransformationNode::setRotation( Maths::Quaternion& rotation )
{
	*((ValueTrigger<Maths::Quaternion>*)(TriggerBase*)m_rotation) = rotation;
}